<?php
namespace Admin\Controller;
use Think\Controller;
class FinanceController extends AuthController {
    
   public function _initialize(){
        parent::_initialize();
    }
    
	/*财务统计页面*/
	public function index(){
	    $finance  = M("finance");
	    $renewal  = M("renewal");
		$domain   = M("domain");
		$group    = M("group");
		
		$month_income  = 0;
		$month_outlay  = 0;
		$month_profits = 0;
		$year_income   = 0;
		$year_outlay   = 0;
		$year_profits  = 0;
		
		$month = date('m');
		$year  = date('Y');
		$today = time();
		/*本月收入支出*/
		$map   =  "FROM_UNIXTIME(paytime,'%m') = $month and FROM_UNIXTIME(paytime,'%Y') = $year";
		$month_income = $finance-> where($map) ->sum('income');
		$month_outlay = $finance -> where($map) ->sum('outlay');
		$month_profits = $month_income - $month_outlay;
		
		/*本年度收入支出*/
		$map2   =  "FROM_UNIXTIME(paytime,'%Y') = $year";
		$year_income = $finance-> where($map2) ->sum('income');
		$year_outlay = $finance -> where($map2) ->sum('outlay');
		$year_profits = $year_income - $year_outlay;
		
		/*遍历查询过去12个月的每个月新增网站，存储为json*/
		$current =time();
		for($i = 0; $i < 13; $i++){
		    $where = "from_unixtime(paytime,'%Y-%m') = from_unixtime($current,'%Y-%m')";
		    $tongji[$i]['year']   = date('Y-m',$current);
			$tongji[$i]['income']    = $finance ->where($where) ->sum('income');
			$tongji[$i]['outlay']    = $finance ->where($where) ->sum('outlay');
			$tongji[$i]['profits']   = $tongji[$i]['income'] - $tongji[$i]['outlay'];
			
			$current = strtotime("-1 Months", $current);
		}
		//$arr = array_merge($arr3,$arr2,$tongji);
		$tongji = json_encode(array_reverse($tongji));
		
		/*全部费用统计*/
		$all_income = $finance ->sum('income');
		$all_outlay = $finance ->sum('outlay');
		$all_profits = $all_income - $all_outlay;
		
		
		/*网站续费统计*/
		/*本月收入支出*/
		$map3   =  "FROM_UNIXTIME(paytime,'%m') = $month and FROM_UNIXTIME(paytime,'%Y') = $year";
		$month_xufei = $renewal-> where($map3) ->sum('num');
		
		/*本年度续费*/
		$map4   =  "FROM_UNIXTIME(paytime,'%Y') = $year";
		$year_xufei = $renewal -> where($map4) ->sum('num');
		
		$all_xufei =  $renewal ->sum('num');
		
		/*遍历查询过去12个月的每个月续费，存储为json*/
		$current2 =time();
		for($i = 0; $i < 13; $i++){
		    $where = "from_unixtime(paytime,'%Y-%m') = from_unixtime($current2,'%Y-%m')";
		    $xufei[$i]['year']   = date('Y-m',$current2);
			$xufei[$i]['num']    = $renewal ->where($where) ->sum('num');		
			$current2 = strtotime("-1 Months", $current2);
		}
		$xufei = json_encode(array_reverse($xufei));
       
	     /*遍历查询过去12个月的每个月新增网站，存储为json*/
		$current5 =time();
		for($i = 0; $i < 12; $i++){
		    $where = "from_unixtime(inputtime,'%Y-%m') = from_unixtime($current5,'%Y-%m')";
		    $dom[$i]['num'] = $domain ->where($where) ->count();
			$dom[$i]['year'] = date('Y-m',$current5);
			$current5 = strtotime("-1 Months", $current5);
		}
		$dom = json_encode(array_reverse($dom));//倒置数组
		$group_info = $group -> field('groupname,webnum')->select();
		$group_num = json_encode($group_info);
		$patten = '/\"webnum\":\"(\d+)\"/';//过滤json中的数字含有双引号的问题
		$group_num = preg_replace($patten ,'"webnum":$1', $group_num);
	   
		$this->assign(array(
		    'month_income'   => $month_income ? $month_income : 0,
			'month_outlay'   => $month_outlay ? $month_outlay : 0,
			'month_profits'  => $month_profits? $month_profits : 0,
			'year_income'    => $year_income ? $year_income : 0,
			'year_outlay'    => $year_outlay ? $year_outlay : 0,
			'year_profits'   => $year_profits ? $year_profits : 0,
			'tongji'         => $tongji,
			'month_xufei'    => $month_xufei ? $month_xufei : 0,
			'year_xufei'     => $year_xufei ? $year_xufei : 0,
			'xufei'          => $xufei,
			'all_xufei'      => $all_xufei ? $all_xufei : 0,
			'all_income'     => $all_income ? $all_income : 0,
			'all_outlay'     => $all_outlay ? $all_outlay : 0,
			'all_profits'    => $all_profits ? $all_profits :0,
			'dom'            => $dom,
			'group_num'      => $group_num,
			'now'   => time(),
		));
        $this->display();
	
	}
 
    /*工作流水*/
    public function workflow(){
	    $finance  = D("finance");
		$so       = I("get.so");
		$pay  = C('pay');
		import("ORG.Util.Page");// 导入分页类

		if(!empty($so) && isset($so) && $so == "search" ){//搜索结果
		     $keyword   =  trim(I('param.keyword','','htmlspecialchars'));
			 $type     =  I('param.type')? I('param.type'): '';
			 $payway    =  I('param.payway')? I('param.payway'): '';
			 $start     =  I('param.start')? strtotime(I('param.start')): '';
			 $end       =  I('param.end')? strtotime(I('param.end')): '';
			 
			 if(!empty($keyword)){
				$where['shiyou']    =  array('like', '%'.$keyword.'%');
				$where['income']    =  array('like', '%'.$keyword.'%');
				$where['outlay']    =  array('like', '%'.$keyword.'%');
				$where['way']       =  array('like', '%'.$keyword.'%');
				$where['content']   =  array('like', '%'.$keyword.'%');
				$where['linkman']   =  array('like', '%'.$keyword.'%');
				$where['_logic']    =  'or';
				$map['_complex']    =  $where;
			}
			if ($start && $end){
				  $map['paytime']=array(array('egt',$start),array('elt',$end)); 
			}elseif($start && $end ==""){
				  $map['paytime']=array(array('egt',$start)); 
			}elseif($start=="" && $end){
				  $map['paytime']=array(array('elt',$end)); 
			}else{
				//$where['price']="";
			};//日期范围
			if(!empty($type)){
			    if(trim($type) == '收入'){
				     $map['income'] = array('neq','');
				}elseif (trim($type) == '支出'){
				    $map['outlay'] = array('neq','');
				}
				
			}
			
			if(!empty($payway)){
				 $map['way'] = array('like', '%'.$payway.'%');;
			}
			$count = $finance  -> where($map) 
							   -> count();;//重置分页总数
		    $page  = new \Think\Page($count,20);//tp3.2新增
		    $show  = $page->show();
		    $limit = $page->firstRow.','.$page->listRows;
			
			$list  = $finance  -> where($map) 
				               -> order("paytime desc, fid desc") 
							   -> limit($limit)
							   -> select();
		
		}else{
		    $count    = $finance->count();
		    $page     = new \Think\Page($count,20);//tp3.2新增
		    $show     = $page->show();
		    $limit    = $page->firstRow.','.$page->listRows;
		    $list = $finance -> order("paytime desc, fid desc") 
				             -> limit($limit)
							 -> select();
		}
		$this->assign(array(
		    'list'   => $list,
			'page'   => $show,
			'pay'    => $pay,
			'now'   => time(),
		));
        $this->display();
    }
	
	/*工作流水编辑*/
    public function workflow_edit(){
	    $fid     = I('get.fid');
		$finance =  D("finance");
		$where   = array('fid' => $fid);
		
		if (!empty($fid)){
			$data    = $finance -> where($where) 
								-> find();
		}else{
		    $this->error("参数传递错误");
		}
		
		$this->assign(array(
		    'data'   => $data,
			'now'   => time(),
		));
        $this->display();
		
	}
	
	/*工作流水更新*/
    public function workflow_update(){
	    $fid     = I('get.fid');
	    $finance = M("finance");
		
        if (IS_POST){
			$data['paytime']   =   strtotime(I('post.paytime','','htmlspecialchars'));
			$data['shiyou']    =   I('post.shiyou','','htmlspecialchars');
			$data['income']    =   I('post.income','','htmlspecialchars');
			$data['outlay']    =   I('post.outlay','','htmlspecialchars');
			$data['way']       =   I('post.way','','htmlspecialchars');
			$data['linkman']   =   I('post.linkman','','htmlspecialchars');
			$data['content']   =   I('post.content','','htmlspecialchars');
			$data['inputtime'] =   I('post.inputtime') ? strtotime(I('post.inputtime')) : time();
		}

	   if (!isset($fid) || $fid ==""){//id为空表示新增
			$count = $finance -> add($data);
			if($count){
				  $this->success("添加成功",U('Finance/workflow'));
			}else{
				  $this->error("添加失败");	
			}
	   }else{//修改信息
	        $where['fid'] = $fid;
	        $count = $finance->where($where) -> save($data);
			if($count){
			    $this->success("修改成功",U('Finance/workflow'));
			}else{
			    $this->error("修改失败");	
			}
	   }
	}
	
	
	/*信息删除*/
	public function finance_del(){
	     $aid     = I('get.fid');//单条
		 $fids    = I('post.fid');//批量
		 $fids    = join(',', $fids);
		 $finance = M("finance");
		if (!empty($fid) && empty($fids) && isset($fid)){
		    $where['fid'] = $fid;
			$count = $finance->where($where)->delete();
		}else{
			$count = $finance->where(array('fid' => array('in', $fids)))->delete();
		}
		if($count){
		    $this->success("删除成功",U('Finance/workflow'));
		}else{
		    $this->error("删除失败");	
		}
	}
	
	
	/*导出报表excel*/
	public function export_workflow(){
	    $finance    = M("finance");	
		$list     = $finance -> order("fid desc") 
							 -> select();

	    Vendor('PHPExcel.PHPExcel');
		$objExcel = new \PHPExcel();
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
		
		$objExcel->getActiveSheet()->setCellValue('A1','ID');
		$objExcel->getActiveSheet()->setCellValue('B1', "打款时间");
		$objExcel->getActiveSheet()->setCellValue('C1', "事由");
		$objExcel->getActiveSheet()->setCellValue('D1', "收入");
		$objExcel->getActiveSheet()->setCellValue('E1', "支出");
		$objExcel->getActiveSheet()->setCellValue('F1', "支付方式");
		$objExcel->getActiveSheet()->setCellValue('G1', "联系人");
		$objExcel->getActiveSheet()->setCellValue('H1', "备注");
		$objExcel->getActiveSheet()->setCellValue('I1', "录入时间");
		
		//设置当前的sheet
		$objExcel->setActiveSheetIndex(0);
		//设置sheet的name
		$objExcel->getActiveSheet()->setTitle('网站流水记录');
		
		//文字对齐方式  锚：bbb
	    $objExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);  
	
		//设置默认行高
        $objExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(30);
		$objExcel->getDefaultStyle()->getFont()->setSize(12);
		
		//设置填充颜色
        $objExcel->getActiveSheet()->getStyle('A1:I1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objExcel->getActiveSheet()->getStyle('A1:I1')->getFill()->getStartColor()->setARGB('#009688');
	   
	   /*设置字体颜色*/
	   $objExcel->getActiveSheet()->getStyle('A1:I1')->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
	   //$objExcel->getActiveSheet()->getStyle('E')->getFont()->getColor()->setARGB('#009688');
	   
	   // 高置列的宽度
		$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
		$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
		$objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
		$objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
		$objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
		
		/*锁定单元格*/
		$objExcel->getActiveSheet()->freezePane("A2");
	    
		$count = count($list);//$driver 为数据库表取出的数据
		for ($i = 2; $i <= $count+1; $i++) {

		    $objExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]['fid']); 
		    $objExcel->getActiveSheet()->setCellValue('B' . $i, date('Y-m-d',$list[$i-2]['paytime'])); 
		    $objExcel->getActiveSheet()->setCellValue('C' . $i, $list[$i-2]['shiyou']); 
		    $objExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]['income']);
			$objExcel->getActiveSheet()->setCellValue('E' . $i, $list[$i-2]['outlay']);
			$objExcel->getActiveSheet()->setCellValue('F' . $i, $list[$i-2]['way']);
			$objExcel->getActiveSheet()->setCellValue('G' . $i, $list[$i-2]['linkman']);
			$objExcel->getActiveSheet()->setCellValue('H' . $i, $list[$i-2]['content']);
			$objExcel->getActiveSheet()->setCellValue('I' . $i, date('Y-m-d',$list[$i-2]['inputtime']));
		}
	   
	   $objExcel->setActiveSheetIndex(); 
	   $excel_name = "全部流水记录".date('Y-m-d',time());
	   header('Content-Type: applicationnd.ms-excel'); 
	   header('Content-Disposition: attachment;filename='.$excel_name.'.xls'); 
	   header('Cache-Control: max-age=0'); 
		
	   $objWriter->save('php://output'); 
	   exit;
	}
	
	/*导入报表excel*/
	public function imports_excel(){
	    $finance = M('finance');
	    $files = $_FILES['exl'];
		
		/*处理上传*/
		$upload = new \Think\Upload();// 实例化上传类  
		$upload->maxSize   =    3145728 ;// 设置附件上传大小  
		$upload->exts      =    array('xls', 'xlsx');// 设置附件上传类型  
		$upload->savePath  =    '/excel/'; // 设置附件上传目录
		$upload->saveName  =    array('date','YmdHis');
		$upload->subName   =   "";//array('date','Ymd');
		$files             =   $upload->upload();
		
		$filename = './Uploads'.$files['exl']['savepath'].$files['exl']['savename'];
        $ext = $files['exl']['ext'];	
		if(!$files) {// 上传错误提示错误信息
              $this->error($upload->getError());
          }else{// 上传成功
               
			  if(strtolower ( $ext )=='xls'){//判断excel表类型为2003还是2007  
                  Vendor("PHPExcel.PHPExcel.Reader.Excel5");   
                  $objReader = \PHPExcel_IOFactory::createReader('Excel5');  
			  }elseif(strtolower ( $ext )=='xlsx'){  
				  Vendor("PHPExcel.PHPExcel.Reader.Excel2007");   
				  $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			  }  
			  $objPHPExcel = $objReader->load($filename,$encode='utf-8');
			  $sheet = $objPHPExcel->getSheet(0);
			  $highestRow = $sheet->getHighestRow(); // 取得总行数
			  $highestColumn = $sheet->getHighestColumn(); // 取得总列数
	
   			  $j=0;
			  for($i=2; $i<=$highestRow; $i++)
			  {
				$num = "";
				$paytime =  $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['paytime'] = strtotime(str_replace('.','-',$paytime));
				$data['shiyou']    = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$num               = floatval($objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue());
				if($num > 0){
					$data['income']= $num;
					$data['outlay']= "";
				}else{
					$data['income']= "";
					$data['outlay']= abs($num);
				}
				$data['way']       = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['linkman']   = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['content']   = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['inputtime'] = time();
	
				$finance->add($data);
				$j++;
			  }
			  unlink($filename);
			  $this->success('导入成功！本次导入流水记录：'.$j.'条信息');
        }
	}
	
	
	 /*续费统计*/
    public function renewal(){
	    $renewal  = D("renewal");
		$so       = I("get.so");
		$pay      = C('pay');
		import("ORG.Util.Page");// 导入分页类
		if(!empty($so) && isset($so) && $so == "search" ){//搜索结果
		     $keyword   =  trim(I('param.keyword','','htmlspecialchars'));
			 $payway    =  I('param.payway')? I('param.payway'): '';
			 $start     =  I('param.start')? strtotime(I('param.start')): '';
			 $end       =  I('param.end')? strtotime(I('param.end')): '';
			 
			 if(!empty($keyword)){
				$where['weburl']    =  array('like', '%'.$keyword.'%');
				$where['webname']   =  array('like', '%'.$keyword.'%');
				$where['num']       =  array('like', '%'.$keyword.'%');
				$where['payway']    =  array('like', '%'.$keyword.'%');
				$where['content']   =  array('like', '%'.$keyword.'%');
				$where['linkman']   =  array('like', '%'.$keyword.'%');
				$where['_logic']    =  'or';
				$map['_complex']    =  $where;
			}
			if ($start && $end){
				  $map['paytime']=array(array('egt',$start),array('elt',$end)); 
			}elseif($start && $end ==""){
				  $map['paytime']=array(array('egt',$start)); 
			}elseif($start=="" && $end){
				  $map['paytime']=array(array('elt',$end)); 
			}else{
				//$where['price']="";
			};//日期范围
			
			if(!empty($payway)){
				 $map['payway'] = array('like', '%'.$payway.'%');
			}
			$count = $renewal  -> where($map) 
							   -> count();;//重置分页总数
		    $page  = new \Think\Page($count,20);//tp3.2新增
		    $show  = $page->show();
		    $limit = $page->firstRow.','.$page->listRows;
			
			$list  = $renewal  -> where($map) 
				               -> order("paytime desc, rid desc") 
							   -> limit($limit)
							   -> select();
		
		}else{
		    $count    = $renewal->count();
			$page     = new \Think\Page($count,20);//tp3.2新增
			$show     = $page->show();
			$limit    = $page->firstRow.','.$page->listRows;
			
			$list     = $renewal -> order("paytime desc, rid desc") 
								 -> limit($limit)
								 -> select();

		}
		
		
		$this->assign(array(
		    'list'   => $list,
			'page'   => $show,
			'pay'    => $pay,
			'now'   => time(),
		));
        $this->display();
    }
	
	/*工作流水编辑*/
    public function renewal_edit(){
	    $rid     = I('get.rid');
		$renewal =  D("renewal");
		$pay  = C('pay');
		$where   = array('rid' => $rid);
		
		if (!empty($rid)){
			$data    = $renewal -> where($where) 
				                -> find();
		}else{
		    $this->error("参数传递错误");
		}
		
		$this->assign(array(
		    'data'   => $data,
			'pay'    => $pay,
			'now'   => time(),
		));
        $this->display();
		
	}
	
	/*续费更新*/
    public function renewal_update(){
	    $rid     = I('get.rid');
	    $renewal = M("renewal");
		
        if (IS_POST){
			$data['webname']   =   I('post.webname','','htmlspecialchars');
			$data['weburl']    =   I('post.weburl','','htmlspecialchars');
			$data['num']       =   I('post.num','','htmlspecialchars');
			$data['payway']    =   I('post.payway','','htmlspecialchars');
			$data['paytime']   =   strtotime(I('post.paytime','','htmlspecialchars'));
			$data['linkman']   =   I('post.linkman','','htmlspecialchars');
			$data['content']   =   I('post.content','','htmlspecialchars');
			$data['inputtime'] =   I('post.inputtime') ? strtotime(I('post.inputtime')) : time();
		}

	   if (!isset($rid) || $rid ==""){//id为空表示新增
			$count = $renewal -> add($data);
			if($count){
				  $this->success("添加成功",U('Finance/renewal'));
			}else{
				  $this->error("添加失败");	
			}
	   }else{//修改信息
	        $where['rid'] = $rid;
	        $count = $renewal->where($where) -> save($data);
			if($count){
			    $this->success("修改成功",U('Finance/renewal'));
			}else{
			    $this->error("修改失败");	
			}
	   }
	}
	
	
	/*信息删除*/
	public function renewal_del(){
	     $rid     = I('get.rid');//单条
		 $rids    = I('post.rid');//批量
		 $rids    = join(',', $rids);
		 $renewal = M("renewal");
		if (!empty($rid) && empty($rids) && isset($rid)){
		    $where['rid'] = $rid;
			$count = $renewal->where($where)->delete();
		}else{
			$count = $renewal->where(array('rid' => array('in', $rids)))->delete();
		}
		if($count){
		    $this->success("删除成功",U('Finance/renewal'));
		}else{
		    $this->error("删除失败");	
		}
	}
	
	/*导入报表excel*/
	public function imports_excel2(){
	    $renewal = M('renewal');
	    $files = $_FILES['exl'];
		
		/*处理上传*/
		$upload = new \Think\Upload();// 实例化上传类  
		$upload->maxSize   =    3145728 ;// 设置附件上传大小  
		$upload->exts      =    array('xls', 'xlsx');// 设置附件上传类型  
		$upload->savePath  =    '/excel/'; // 设置附件上传目录
		$upload->saveName  =    array('date','YmdHis');
		$upload->subName   =   "";//array('date','Ymd');
		$files             =   $upload->upload();
		
		$filename = './Uploads'.$files['exl']['savepath'].$files['exl']['savename'];
        $ext = $files['exl']['ext'];	
		if(!$files) {// 上传错误提示错误信息
              $this->error($upload->getError());
          }else{// 上传成功
               
			  if(strtolower ( $ext )=='xls'){//判断excel表类型为2003还是2007  
                  Vendor("PHPExcel.PHPExcel.Reader.Excel5");   
                  $objReader = \PHPExcel_IOFactory::createReader('Excel5');  
			  }elseif(strtolower ( $ext )=='xlsx'){  
				  Vendor("PHPExcel.PHPExcel.Reader.Excel2007");   
				  $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			  }  
			  $objPHPExcel = $objReader->load($filename,$encode='utf-8');
			  $sheet = $objPHPExcel->getSheet(0);
			  $highestRow = $sheet->getHighestRow(); // 取得总行数
			  $highestColumn = $sheet->getHighestColumn(); // 取得总列数
	
   			  $j=0;
			  for($i=2; $i<=$highestRow; $i++)
			  {
				$data['webname']   = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['weburl']    = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['num']       = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$paytime =  $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['paytime']   = strtotime(str_replace('.','-',$paytime));
				$data['payway']    = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['linkman']   = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['content']   = $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['inputtime'] = time();
	
				$renewal->add($data);
				$j++;
			  }
			  unlink($filename);
			  $this->success('导入成功！本次导入流水记录：'.$j.'条信息');
        }
	}
	
	/*导出报表excel*/
	public function export_renewal(){
	    $renewal  = M('renewal');
	    $list     = $renewal -> order("rid desc") 
							 -> select();

	    Vendor('PHPExcel.PHPExcel');
		$objExcel = new \PHPExcel();
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
		
		$objExcel->getActiveSheet()->setCellValue('A1','ID');
		$objExcel->getActiveSheet()->setCellValue('B1', "网站名称");
		$objExcel->getActiveSheet()->setCellValue('C1', "网站域名");
		$objExcel->getActiveSheet()->setCellValue('D1', "续费金额");
		$objExcel->getActiveSheet()->setCellValue('E1', "支付方式");
		$objExcel->getActiveSheet()->setCellValue('F1', "续费时间");
		$objExcel->getActiveSheet()->setCellValue('G1', "联系人");
		$objExcel->getActiveSheet()->setCellValue('H1', "备注");
		$objExcel->getActiveSheet()->setCellValue('I1', "录入时间");
		
		//设置当前的sheet
		$objExcel->setActiveSheetIndex(0);
		//设置sheet的name
		$objExcel->getActiveSheet()->setTitle('网站续费记录');
		
		//文字对齐方式  锚：bbb
	    $objExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);  
	
		//设置默认行高
        $objExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(30);
		$objExcel->getDefaultStyle()->getFont()->setSize(12);
		
		//设置填充颜色
        $objExcel->getActiveSheet()->getStyle('A1:I1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $objExcel->getActiveSheet()->getStyle('A1:I1')->getFill()->getStartColor()->setARGB('#009688');
	   
	   /*设置字体颜色*/
	   $objExcel->getActiveSheet()->getStyle('A1:I1')->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
	   //$objExcel->getActiveSheet()->getStyle('E')->getFont()->getColor()->setARGB('#009688');
	   
	   // 高置列的宽度
		$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
		$objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(13);
		$objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
		$objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
		$objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
		$objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
		
		/*锁定单元格*/
		$objExcel->getActiveSheet()->freezePane("A2");
	    
		$count = count($list);//$driver 为数据库表取出的数据
		for ($i = 2; $i <= $count+1; $i++) {

		    $objExcel->getActiveSheet()->setCellValue('A' . $i, $list[$i-2]['rid']); 
		    $objExcel->getActiveSheet()->setCellValue('B' . $i, $list[$i-2]['webname']); 
		    $objExcel->getActiveSheet()->setCellValue('C' . $i, $list[$i-2]['weburl']); 
		    $objExcel->getActiveSheet()->setCellValue('D' . $i, $list[$i-2]['num']);
			$objExcel->getActiveSheet()->setCellValue('E' . $i, $list[$i-2]['payway']);
			$objExcel->getActiveSheet()->setCellValue('F' . $i, date('Y-m-d',$list[$i-2]['paytime']));
			$objExcel->getActiveSheet()->setCellValue('G' . $i, $list[$i-2]['linkman']);
			$objExcel->getActiveSheet()->setCellValue('H' . $i, $list[$i-2]['content']);
			$objExcel->getActiveSheet()->setCellValue('I' . $i, date('Y-m-d',$list[$i-2]['inputtime']));
		}
	   
	   $objExcel->setActiveSheetIndex(); 
	   $excel_name = "全部续费记录".date('Y-m-d',time());
	   header('Content-Type: applicationnd.ms-excel'); 
	   header('Content-Disposition: attachment;filename='.$excel_name.'.xls'); 
	   header('Cache-Control: max-age=0'); 
		
	   $objWriter->save('php://output'); 
	   exit;
	}
	
	/*ajax检测查看密码权限*/
	public function check_pwd(){
	    $pwd = I("post.pwd");
		$user2 = M("user2");
		$userid = $_SESSION['Aid'];
		$admintype = $_SESSION['Aadmintype'];
		$data = 0;
		$where['id'] = $userid;
		if ($admintype == 0){
			$arr = $user2 ->where($where) ->find();
			if($arr){
				 if(md5($pwd) == $arr['password'] && $arr['admintype'] == 0){
					$data = 1;
				 }else{
					 $data = 0;
				 }
			}else{
				$data = 0;
			}
	    }else{//非超级管理员禁止
		    $data = 0;
		}
		$this->ajaxReturn($data); 
	}
	

}